package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.NoticeDetailActivity;
import com.xianbeiying.app.ckgl.bean.NoticesBean;
import com.xianbeiying.app.ckgl.util.DataUtil;

/**
 * 公告信息数据适配
 */
public class NoticeListdapter extends BaseAdapter {

    private Context context;
    private NoticesBean noticesBean;
    public NoticeListdapter(Context context, NoticesBean noticesBean) {
        this.context = context;
        this.noticesBean=noticesBean;
    }

    @Override
    public int getCount() {
        return noticesBean.getNOTICE().size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_notice, parent, false);
            holder = new ViewHolder();
            holder.view_notice_line1 = (View) convertView.findViewById(R.id.view_notice_line1);
            holder.view_notice_line2 = (View) convertView.findViewById(R.id.view_notice_line2);
            holder.view_notice_line3 = (View) convertView.findViewById(R.id.view_notice_line3);
            holder.tv_notice_date = (TextView) convertView.findViewById(R.id.tv_notice_date);
            holder.tv_notive_title = (TextView) convertView.findViewById(R.id.tv_notive_title);
            holder.tv_notice_time = (TextView) convertView.findViewById(R.id.tv_notice_time);
            holder.img_notice_right = (ImageView) convertView.findViewById(R.id.img_notice_right);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.view_notice_line1.setVisibility(View.GONE);
        holder.view_notice_line3.setVisibility(View.VISIBLE);
        if (position == 0) {
            holder.view_notice_line1.setVisibility(View.VISIBLE);
        }
        if ((position+1)== noticesBean.getNOTICE().size()) {
            holder.view_notice_line3.setVisibility(View.GONE);
        }
        holder.tv_notice_date.setText(DataUtil.paserTime(noticesBean.getNOTICE().get(position).getNoticeTime()+"","yyyy-MM-dd"));
        holder.tv_notice_time.setText(DataUtil.paserTime(noticesBean.getNOTICE().get(position).getNoticeTime()+"","HH:mm:ss"));
        holder.tv_notive_title.setText(noticesBean.getNOTICE().get(position).getTitle());
        holder.img_notice_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转详情
                context.startActivity(new Intent().setClass(context, NoticeDetailActivity.class).putExtra("noticesBean",noticesBean.getNOTICE().get(position)));
            }
        });
        return convertView;
    }

    private class ViewHolder{
        View view_notice_line1, view_notice_line2, view_notice_line3;
        ImageView img_notice_right;
        TextView tv_notive_title;
        TextView tv_notice_date, tv_notice_time;
    }
}
